<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
  <title>Berkeley DB Java Edition Examples</title>
</head>
<body>

<p align=center>
<img src="images/Oracle_BerkeleyDB_small.png" alt="Oracle">
</p>

<center><h1>Berkeley DB Java Edition<br>Examples</h1></center>

<p>The JE distribution comes with examples that illustrate:</p>
<ul>
<li>using the JE programming interfaces to perform basic database
operations</li>
<li>scenarios from the Getting Started Guide and Writing Transactional
Applications</li>
<li>using the Direct Persistence Layer to implement common SQL
queries</li>
<li>using JE High Availability</li>
</ul>
<hr noshade="noshade" size="1">
<a href="#examples">Building and running JE examples</a><br>
<a href="#basicexample">Basic Example</a><br>
<a href="#gsg">Getting Started Examples</a><br>
<a href="#txn">Writing Transactional Applications Examples</a><br>
<a href="#sql">Translating SQL Queries</a><br>
<a href="#exampleList">Examples List</a><br>

<h3><a name="basicexample">Building and Running a Basic Example</a></h3>

<p>Compiling and running a simple example can serve as a sanity check of the
installation.  Follow the instructions below to compile and run the
PersonExample.</p>

<p>You can find the source for this example at:</p>

<blockquote>
  <pre>JE_HOME/examples/persist/PersonExample.java</pre>
</blockquote>

<p>Assuming you have installed the JavaSE JDK and have verified that you
have a working Java compiler, you can build <code>PersonExample</code>
as follows.</p>

<ol>
  <li>Change to the
    <pre>JE_HOME/examples</pre> directory.</li>
  <br>
  <li>Set your CLASSPATH to include both <pre>JE_HOME/lib/je-M.N.P.jar</pre>
      and the <pre>JE_HOME/examples</pre> directory.</li>
  <br>
  <li>Compile <code>PersonExample.java</code> with the following command:
    <pre>javac persist/PersonExample.java</pre> or on Windows:
    <pre>javac persist\PersonExample.java</pre>
  </li>
</ol>

<p>To run <code>PersonExample</code> , use the following command, specifying an
environment directory for the data generated by the example:</p>

<blockquote>
  <pre>java persist.PersonExample -h &lt;environment directory&gt;</pre>
</blockquote>

<p>For example, using <code>"."</code> for the second parameter will write
the database files into the current directory. You'll notice that a
<code>00000000.jdb</code> file and and <code>je.lck</code> file are
created.  This is the first log file in the environment and a lock
file. If you need to delete the environment for running a different
example, simply delete these two files.</p>

<p>When you run the program you'll see the following output.  While this is not
a very entertaining program, it is enough to test that you have installed JE
correctly.</p>

<pre>222-22-2222 Jack Smith
333-33-3333 Mary Smith</pre>

<p>The other JE examples are compiled and run similarly.  How to run
the examples from the Getting Started Guide and Writing Transactional
Applications is described in the sections below, as well as how to run
the Translating SQL Queries examples.  Instructions for running other
examples are contained in the example's source file.</p>

<h3><a name="gsg">Running the Getting Started Examples</a></h3>

<p>As described in the <em>Berkeley DB Java Edition Getting Started
Guide</em>, the final examples in every chapter exist in the JE
package. You can build and run these examples as follows:</p>

<ul>
<li>Change to the
  <pre>JE_HOME/examples</pre>
directory.</li>
<br>
<li>Set your CLASSPATH to include both
  <pre>JE_HOME/lib/je-M.N.P.jar</pre>
and the
  <pre>JE_HOME/examples</pre>
directories.</li>
<br>
<li>Compile the Getting Started examples with the following commands:
  <pre>javac je/gettingStarted/*.java</pre>
or on Windows:
  <pre>javac je\gettingStarted\*.java</pre>
</li>
<li>Make a directory to contain your database environment:
  <pre>mkdir gsgEnv</pre>
</li>
<li>Load your sample database:
  <pre>java je.gettingStarted.ExampleDatabasePut -h gsgEnv -i je/gettingStarted/inventory.txt  -v je/gettingStarted/vendors.txt</pre>
</li>
<li>Perform queries against your database. To see everything in the
database, use:
  <pre>java je.gettingStarted.ExampleInventoryRead -h gsgEnv</pre>
To perform a query based on an inventory item's name, use:
  <pre>java je.gettingStarted.ExampleInventoryRead -h gsgEnv -s Upo</pre>
</li>
</ul>

<h3><a name="txn">Running the Writing Transactional Applications
Examples</a></h3>

<p>The examples in <em>Writing Transactional Applications with
Berkeley DB, Java Edition guide</em> exist in the JE package. You
can build and run these examples as follows:</p>
<ul>
<li>Change to the
  <pre>JE_HOME/examples</pre>
directory.</li>
<br>
<li>Set your CLASSPATH to include both
  <pre>JE_HOME/lib/je-M.N.P.jar</pre>
and the
  <pre>JE_HOME/examples</pre>
directories.</li>
<br>
<li>Compile the Transactional Applications examples with the following commands:
  <pre>javac je/txn/*.java</pre>
or on Windows:
  <pre>javac je\txn\*.java</pre>
</li>
<li>Make a directory to contain your database environment:
  <pre>mkdir txnEx</pre>
</li>
<li>Run the transactional example.
  <pre>java je.txn.TxnGuide -h txnEx</pre>
</li>
</ul>

<h3><a name="sql">Running the Translating SQL Query Examples</a></h3>

<p>This example shows how some common SQL queries can be implemented
using the Direct Persistence Layer. It's meant to help users who are
more familiar with SQL translate those approaches to the DPL.  These
queries include:</p>
<dl>
<dt>Basic data retrieval:</dt><br>
<dd><code>SELECT * FROM tab ORDER BY col ASC;</code></dd><br>

<dt>A prefix query:</dt><br>
<dd><code>SELECT * FROM tab WHERE col LIKE 'prefix%';</code></dd><br>

<dt>A range query, where the data type of A (as well as B) may be an
int, a float, a String, etc:</dt><br>
<dd><code>SELECT * FROM tab WHERE col gt;= A AND col &lt;= B;</code></dd><br>

<dt>An equi-join on a single primary database:</dt><br>
<dd><code>SELECT * FROM tab WHERE col1 = A AND col2 =B;</code></dd><br>

<dt>An equi-join on two primary databases combined
with a filtering on "t2.col2". Note that if "t2.col2" is a secondary key, the
filtering does a index lookup. Otherwise the filtering is done through
database scanning:</dt><br>
<dd><code>SELECT t1.* FROM table1 t1, table2 t2 WHERE t1.col1 = t2.col1
AND t2.col2 = A;</code></dd>
</dl>
<p>You can build and run these examples as follows:</p>
<ul>
<li>Change to the
  <pre>JE_HOME/examples</pre>
directory.</li>
<br>
<li>Set your CLASSPATH to include both
  <pre>JE_HOME/lib/je-M.N.P.jar</pre>
and the
  <pre>JE_HOME/examples</pre>
directories.</li>
<br>
<li>Compile the SQL query examples with the following commands:
  <pre>javac persist/sqlapp/*.java</pre>
or on Windows:
  <pre>javac persist\sqlapp\*.java</pre>
</li>
<li>Make a directory to contain your database environment:
  <pre>mkdir sqlEnv</pre>
</li>
<li>Run the SQL query examples:
  <pre>java persist.sqlapp.SQLApp -h sqlEnv</pre>
To delete database files after the example exits, do:
  <pre>java persist.sqlapp.SQLApp -h sqlEnv -d</pre>
</li>
</ul>
<h3><a name="exampleList">List of Examples</a></h3>
<table border="1" cellpadding="4">
<tr>
    <th>Example</th>
    <th>Location</th>
    <th>API</th>
    <th>Description</th>
</tr>
<tr>
    <td>Getting Started Guide</td>
    <td>examples/persist/ gettingStarted</td>
    <td>DPL</td>
    <td>scenarios using the Direct Persistence Layer from the Getting Started
        Guide</td>
</tr>
<tr>
    <td>Writing Transactional Applications</td>
    <td>examples/persist/txn</td>
    <td>DPL</td>
    <td>scenarios using the Direct Persistence Layer from Writing Transactional
        Applications</td>
</tr>
<tr>
    <td>Writing Transactional Applications</td>
    <td>examples/je/txn</td>
    <td>Base</td>
    <td>scenarios using the Base API from Writing Transactional
        Applications</td>
</tr>
<tr>
    <td>Translating SQL Queries</td>
    <td>examples/persist/sqlApp</td>
    <td>DPL</td>
    <td>shows how some common SQL queries can be implemented using the Direct
        Persistence Layer</td>
</tr>
<tr>
    <td>PersonExample</td>
    <td>examples/persist</td>
    <td>DPL</td>
    <td>demonstrates basic use of the Direct Persistence Layer</td>
</tr>
<tr>
    <td>ScalaPersonExample</td>
    <td>examples/persist</td>
    <td>DPL</td>
    <td>demonstrates using JE with the Scala programming language</td>
</tr>
<tr>
    <td>EventExample EventExampleDPL</td>
    <td>examples/persist</td>
    <td>DPL</td>
    <td>contrasts the Base API and the Direct Persistence Layer with an
        example of storing event objects</td>
</tr>
<tr>
    <td>CustomKeyOrderExample</td>
    <td>examples/persist</td>
    <td>DPL</td>
    <td>shows how to use a Comparable to specify key order</td>
</tr>
<tr>
    <td>DplDump</td>
    <td>examples/persist</td>
    <td>DPL</td>
    <td>dumps objects stored using the Direct Persistence Layer in XML
        format</td>
</tr>
<tr>
    <td>HelloDatabaseWorld</td>
    <td>examples/collections/hello</td>
    <td>Collections</td>
    <td>trivial example using the Collections API</td>
</tr>
<tr>
    <td>AccessExample</td>
    <td>examples/collections/access</td>
    <td>Collections</td>
    <td>reimplementation of the Base API AccessExample using the Collections
        API</td>
</tr>
<tr>
    <td>Shipments</td>
    <td>examples/collections/ship</td>
    <td>Collections</td>
    <td>series of examples based on a shipment database</td>
</tr>
<tr>
    <td>SimpleExample</td>
    <td>examples/je</td>
    <td>Base</td>
    <td>does basic data insertion and retrieval</td>
</tr>
<tr>
    <td>BindingExample</td>
    <td>examples/je</td>
    <td>Base</td>
    <td>shows how to use com.sleepycat.bind to convert between Java objects and
        JE data records</td>
</tr>
<tr>
    <td>SecondaryExample</td>
    <td>examples/je</td>
    <td>Base</td>
    <td>illustrates the use of secondary indices</td>
</tr>
<tr>
    <td>SequenceExample</td>
    <td>examples/je</td>
    <td>Base</td>
    <td>demonstrates the use of Sequence objects</td>
</tr>
<tr>
    <td>ToManyExample</td>
    <td>examples/je</td>
    <td>Base</td>
    <td>shows how to use multi-key secondary indices to support many-many and
        one-many primary/secondary key relationships</td>
</tr>
<tr>
    <td>MeasureInsertSize</td>
    <td>examples/je</td>
    <td>Base</td>
    <td>inserts a given set of key/value pairs in order to measure the disk
        space consumed by a given data set</td>
</tr>
<tr>
    <td>JCA</td>
    <td>examples/jca</td>
    <td>Base</td>
    <td>shows how to use the J2EE Connector Architecture with JE</td>
</tr>
<tr>
    <td>StockQuotes</td>
    <td><a href="examples/je/rep/quote/package-summary.html">examples/je/rep/quote</a></td>
    <td>High Availability/Replication</td>
    <td>shows how to use BDB JE High Availability</td>
</tr>
</table>

<hr noshade="noshade" size="1">
<p><a href="sleepycat/legal.html">Copyright (c) 2002-2010</a> Oracle.
All rights reserved.</font></p>

</body>
</html>
